﻿function webPopupLayer() {
    this.cssClassName = "popuplayer";
    this.classId = 0;
    this.parentId = 0;
    //this.level = 0;
    this.httpUrl = "/Ajax/ShareCode.ashx";
    this.mainDivId = "div_Main_pl";
    this.currSelShowDivId = "div_CurrSelText_pl";
    this.contentId = "ul_Content_pl";
    this.contentColId = "ul_ContentCol_{0}_pl";
    this.selected = { id: "", name: "" };
    this.callbackHandler = null;
    this.client = {
        dataType: "",
        saveSelItemEleId: "",
        renderSelItemEleId: ""
    };
    this.lvlText = {};
}
webPopupLayer.codeClassEnum = {};
webPopupLayer.codeClassEnum.fish = 15;
webPopupLayer.codeClassEnum.area = 13;
webPopupLayer.prototype = {
    show: function (dataType, renderSelItemEleId, saveSelItemEleId) {
        this.client.dataType = dataType;
        this.client.renderSelItemEleId = renderSelItemEleId;
        this.client.saveSelItemEleId = saveSelItemEleId;
        document.body.focus();
        switch (dataType.toLowerCase()) {
            case "cate": this.render(webPopupLayer.codeClassEnum.fish, 0, 0); break;
            case "area": this.render(webPopupLayer.codeClassEnum.area, 0, 0); break;
        }

    },
    render: function (classId, parentId, level, name, currEle) {
        if (name == null || name == undefined) {
            name = "";
        }
        this.selectedId = { code: "", name: "" };
        if (arguments.length >= 1) {
            this.classId = classId;
        }
        if (arguments.length >= 2) {
            this.parentId = parentId;
        }
        if (currEle != undefined && currEle != null) {
            var eleList = currEle.parentNode.getElementsByTagName("p");
            for (var i = 0, len = eleList.length; i < len; i++) {
                eleList[i].className = "";
            }
            currEle.className = "curr";
        }

        this.loadData(level, name);
    },
    initContent: function () {
        if (jDoc(this.mainDivId) == null) {
            var html = [];
            html.push("<div id='" + this.mainDivId + "' class='" + this.cssClassName + "'>");
            html.push("<div id='" + this.currSelShowDivId + "' style='border-bottom:1px solid #6694E3;'>&nbsp;</div>");
            html.push("<div style='margin:3px 0px 5px 0px;'><input type='button' onclick='oPopupLayer.showSelectedId(1)' value='&nbsp;确&nbsp;&nbsp;定&nbsp;'/>");
            html.push("&nbsp;&nbsp;");
            html.push("<input type='button' onclick='webPB.close()' value='&nbsp;取&nbsp;&nbsp;消&nbsp;'/>");
            html.push("&nbsp;&nbsp;");
            html.push("<input type='button' onclick='oPopupLayer.showSelectedId(0)' value='&nbsp;清&nbsp;&nbsp;除&nbsp;'/>");
            html.push("</div>");
            html.push("<ul id='" + this.contentId + "'></ul><div class='clear'></div>");
            html.push("</div>");
            webPB.showDialog(html.join(""));
        }
    },
    loadData: function (level, name) {
        var self = this;
        var postData = { classId: this.classId, parentId: this.parentId };
        $.post(this.httpUrl, postData, function (jsonData) {
            self.get(jsonData, level, name);
        }, "json");
    },
    get: function (data, level, name) {
        this.removeItem(level);
        if (data.length > 0) {
            var col = document.createElement("li");
            col.id = String.format(this.contentColId, level);
            col.innerHTML = this.itemHtml(data, level, name);
            this.initContent();
            jDoc(this.contentId).appendChild(col);
        } else {
            this.selected.id = this.parentId;
            this.selected.name = name;
        }
        this.lvlText[level] = name;
        this.showSelectedText(level);
        webPromptBox.resize();
    },
    showSelectedText: function (level) {
        var index = level + 1;
        while (this.lvlText[index]) {
            delete this.lvlText[index];
            index += 1;
        }
        index = 1;
        var text = "";
        while (this.lvlText[index]) {
            if (index > level) { break; }
            text += (index == 1 ? "" : "-->") + this.lvlText[index];
            index++;
        }
        jDoc(this.currSelShowDivId).innerHTML = text;
    },
    removeItem: function (level) {
        if (level == 0) return;
        var index = level;
        var elem = null;
        var elem = jDoc(String.format(this.contentColId, level));
        while (elem) {
            elem.parentNode.removeChild(elem);
            index++;
            elem = jDoc(String.format(this.contentColId, index));
        }
    },
    itemHtml: function (data, level, name) {
        var html = [];
        var index = 0, len = data.length;
        for (; index < len; index++) {
            html.push("<p onclick='oPopupLayer.render(" + this.classId + ", " + data[index]["CodeID"] + "," + (level) + "+ 1,\"" + data[index]["Name"].replace('"', '') + "\",this);'>");
            html.push("<a>" + data[index]["Name"] + "</a>");
            html.push("</p>");
        }
        return html.join("");
    },
    showSelectedId: function (flag) {
        if (flag == 1 && this.selected.id == "") {
            alert('请选择最后一级');
            return;
        }
        jDoc(this.client.renderSelItemEleId).value = jDoc(this.currSelShowDivId).innerHTML.replace(/&gt;/ig, ">"); // this.selected.name;
        jDoc(this.client.saveSelItemEleId).value = this.selected.id;

        if (this.callbackHandler != null) {
            this.callbackHandler(this.classId, this.selected.id, this.selected.name);
        }
        this.selected = { id: "", name: "" };
        this.parentId = "";
        this.client.renderSelItemEleId = "";
        this.client.saveSelItemEleId = "";

        webPB.close();
    }
}

var oPopupLayer = new webPopupLayer();
